package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.apple.util.RandomUtility;
import yxy.cherry.data.bean.actflopturntable;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopTurntableRandom;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * 翻牌 - 活跃任务 夺宝
 * @author Dell
 *
 */
@MessageType(proto = Command.ActFlopCardTurn, classtype = ActiveMessage.ActFlopCardTurnRequest.class)
public class CMD_Active_翻牌_活跃任务_Turn extends Command<ActiveMessage.ActFlopCardTurnRequest> {

	private static final Logger logger = Logger.create(CMD_Active_翻牌_活跃任务_Turn.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardTurnRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int type = request.getType();
		ActiveMessage.ActFlopCardTurnResponse.Builder response = ActiveMessage.ActFlopCardTurnResponse.newBuilder();
		
		ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(type != ActiveModule_翻牌.type1 && type != ActiveModule_翻牌.type10) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		Currency costcur = Currency.create(CurrencyType.CT_物品, 1040, 1 * type);
		boolean cost = new CurrencyHandle().costCheck(playercode, costcur);
		if(!cost) {
			send(messageSource, response, new Result(Result.CostFail, CurrencyType.CT_物品.getNumber(), 1040));
			return;
		}
		
		ArrayList<FlopTurntableRandom> ttrs = new ArrayList<FlopTurntableRandom>();
 		Map<String, actflopturntable> afttmap = ActDataCenter.getAllData(actflopturntable.class, playercode);
		for(actflopturntable data : afttmap.values()) {
			FlopTurntableRandom ttr = new FlopTurntableRandom();
			ttr.setPostion(data.getPosition());
			ttr.setRewardtype(data.getRewardtype());
			ttr.setRewardid(data.getRewardid());
			ttr.setRewardnum(data.getRewardnum());
			ttr.setPercent(data.getPercent());
			ttrs.add(ttr);
		}
	
		new CurrencyHandle().cost(playercode, CCReason.活动_翻牌, costcur);
		Currency nowcur = Item.get(playercode, CurrencyType.CT_物品, 1040);
		response.setItemnum(nowcur.getNumber());
		
		ArrayList<Currency> curs = new ArrayList<Currency>();
		ArrayList<Integer> positions = new ArrayList<Integer>();
		
		for(int i = 0; i < type; i++) {
			FlopTurntableRandom ttr = RandomUtility.randomWeight(ttrs);
			positions.add(ttr.getPostion());
			curs.add(ttr.getCur());
			if(curs.size() == 0) {
				System.out.println(ttr);
			}
		}
		
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		response.addAllPositions(positions);
		response.addAllDisplay(items);
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_翻牌, curs);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActFlopCardTurnResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
